package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.LogisticsCompanyDTO;
import com.itheima.dto.LogisticsPriceDTO;
import com.itheima.mapper.LogisticsCompanyMapper;
import com.itheima.mapper.LogisticsPriceMapper;
import com.itheima.pojo.LogisticsCompany;
import com.itheima.service.LogisticsCompanyService;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class LogisticsCompanyServiceImpl extends ServiceImpl<LogisticsCompanyMapper, LogisticsCompany>
        implements LogisticsCompanyService {

    private final LogisticsCompanyMapper companyMapper;
    private final LogisticsPriceMapper priceMapper;

    @Override
    public Page<LogisticsCompanyDTO> getCompanyPage(Page<LogisticsCompanyDTO> page, LogisticsCompanyDTO dto) {
        PageHelper.startPage((int)page.getCurrent(), (int)page.getSize());

        LambdaQueryWrapper<LogisticsCompany> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LogisticsCompany::getIsDel, 1);

        if (dto != null) {
            if (StringUtils.isNotBlank(dto.getCompanyName())) {
                wrapper.like(LogisticsCompany::getCompanyName, dto.getCompanyName());
            }
            if (StringUtils.isNotBlank(dto.getLinkUser())) {
                wrapper.like(LogisticsCompany::getLinkUser, dto.getLinkUser());
            }
            if (dto.getMinPrice() != null) {
                wrapper.ge(LogisticsCompany::getMinPrice, dto.getMinPrice());
            }
        }

        List<LogisticsCompany> list = this.list(wrapper);
        PageInfo<LogisticsCompany> pageInfo = new PageInfo<>(list);

        Page<LogisticsCompanyDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), pageInfo.getTotal());
        dtoPage.setRecords(convertToDtoList(list));
        return dtoPage;
    }

    @Override
    public LogisticsCompanyDTO getCompanyWithPrices(Integer id) {
        LogisticsCompany company = this.getById(id);
        if (company == null || company.getIsDel() == 2) {
            return null;
        }

        LogisticsCompanyDTO dto = new LogisticsCompanyDTO();
        BeanUtils.copyProperties(company, dto);
        dto.setIsValid(company.getIsDel());

        // 查询关联的价格
        List<LogisticsPriceDTO> prices = priceMapper.selectPricesByCompanyId(id);
        dto.setPriceList(prices);

        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCompany(LogisticsCompany company) {
        if (company.getId() == null) {
            company.setCreateTime(LocalDateTime.now());
            company.setIsDel(1);
            return this.save(company);
        } else {
            company.setUpdateTime(LocalDateTime.now());
            return this.updateById(company);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCompany(LogisticsCompany company) {
        company.setUpdateTime(LocalDateTime.now());
        return this.updateById(company);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCompany(Integer id) {
        LogisticsCompany company = new LogisticsCompany();
        company.setId(id);
        company.setIsDel(2);
        company.setUpdateTime(LocalDateTime.now());
        return this.updateById(company);
    }


    private LogisticsCompanyDTO convertToDto(LogisticsCompany company) {
        LogisticsCompanyDTO dto = new LogisticsCompanyDTO();
        BeanUtils.copyProperties(company, dto);
        dto.setIsValid(company.getIsDel());
        return dto;
    }

    private List<LogisticsCompanyDTO> convertToDtoList(List<LogisticsCompany> list) {
        return list.stream().map(this::convertToDto).collect(Collectors.toList());
    }
}